package com.kitty.game.item.service.handler;

import com.kitty.game.role.model.Role;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.game.config.OnlineMall;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.bag.message.RespIconCartoon;
import com.kitty.game.item.message.vo.MergeItemId;
import com.kitty.mina.message.MessagePusher;
import com.kitty.game.base.service.BagService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;


@Component("MergeHandler")
@Slf4j
public class MergeHandler extends MergerItemHandler {
    private static final String LINGLONG_NAME = "中级血玲珑";
    private static final String TARGET_NAME = "中级血玲珑·融合";
    private static final int NUM = 10;

    protected String getLinglongName() {
        return LINGLONG_NAME;
    }

    protected String getTargetName() {
        return TARGET_NAME;
    }

    protected int getNum() {
        return NUM;
    }

    @Override
    public void mergeItem(Role role, List<MergeItemId> itemIdList) {
        for (MergeItemId mergeItemId : itemIdList) {
            String[] itemIds = mergeItemId.getItemId().split("\\|");
            if (itemIds.length < getNum()) {
                return;
            }
            for (String itemId : itemIds) {
                int equipId = Integer.parseInt(itemId);
                RoleEquip roleEquip = equipService.getEquipById(role, equipId);
                if (roleEquip == null || !roleEquip.getName().equals(getLinglongName())) {
                    return;
                }
            }
            for (String itemId : itemIds) {
                RoleEquip roleEquip = equipService.getEquipById(role, Integer.parseInt(itemId));
                equipService.delEquipNum(role, roleEquip, 1);
            }

            SpringUtils.getBean(BagService.class).addRoleEquip(role, false, 1, getOnlineMall());
            MessagePusher.pushMessage(role, new RespIconCartoon(getTargetName()));
        }

        pushResult(role);
    }

    protected OnlineMall getOnlineMall() {
        OnlineMall onlineMall = new OnlineMall();
        onlineMall.setName(getTargetName());
        onlineMall.setItemtype((short) 2);
        onlineMall.setType(1048626);
        return onlineMall;
    }


}
